import 'package:flutter/material.dart';
import 'package:flutter_demo1/navigator/bottom_navigator.dart';
import 'package:flutter_demo1/page/login_page.dart';
import 'package:flutter_demo1/page/registration_page.dart';
import 'package:flutter_demo1/page/video_detail_page.dart';

typedef RouteChangeListener(RouteStatusInfo current, RouteStatusInfo? pre);

pageWrap(Widget child) {
  return MaterialPage(key: ValueKey(child.hashCode), child: child);
}

//获取routeStatus在栈中的位置
int getPageIndex(List<MaterialPage> pages, RouteStatus routeStatus) {
  for (int i = 0; i < pages.length; i++) {
    MaterialPage page = pages[i];
    if (getStatus(page) == routeStatus) {
      return i;
    }
  }
  return -1;
}

//自定义路由封装,路由状态
enum RouteStatus { login, registration, home, detail, unknown }

//获取page对应的RouteStatus
RouteStatus getStatus(MaterialPage page) {
  if (page.child is LoginPage) {
    return RouteStatus.login;
  } else if (page.child is RegistrationPage) {
    return RouteStatus.registration;
  } else if (page.child is BottomNavigator) {
    return RouteStatus.home;
  } else if (page.child is VideoDetailPage) {
    return RouteStatus.detail;
  } else {
    return RouteStatus.unknown;
  }
}

//路由的信息
class RouteStatusInfo {
  final RouteStatus routeStatus;
  final Widget page;

  RouteStatusInfo(this.routeStatus, this.page);
}

class HiNavigator extends _RouteJumpListener {
  static final HiNavigator _instance = HiNavigator._();
  RouteJumpListener? _routeJump;
  final List<RouteChangeListener> _listener = [];
  RouteStatusInfo? _current;

  //底部的tab
  RouteStatusInfo? _bottomTab;

  HiNavigator._();

  static HiNavigator getInstance() {
    return _instance;
  }

  ///首页切换监听,用于监听底部4个按钮的切换
  void onButtonTabChange(int index, Widget page) {
    _bottomTab = RouteStatusInfo(RouteStatus.home, page);
    _notify(_bottomTab!);
  }

  ///注册路由的跳转逻辑
  void registerRouteJump(RouteJumpListener routeJumpListener) {
    _routeJump = routeJumpListener;
  }

  void addListener(RouteChangeListener listener) {
    if (!_listener.contains(listener)) {
      _listener.add(listener);
    }
  }

  void removeListener(RouteChangeListener listener) {
    _listener.remove(listener);
  }

  @override
  void onJumpTo(RouteStatus routeStatus, {Map<String, dynamic>? args}) {
    if (null != _routeJump?.onJumpTo) {
      _routeJump!.onJumpTo!(routeStatus, args: args);
    }
  }

  //通知路由页面变化
  void notify(List<MaterialPage> currentPages, List<MaterialPage> prePages) {
    if (currentPages == prePages) return;
    var current = RouteStatusInfo(
      getStatus(currentPages.last),
      currentPages.last.child,
    );
    //具体的通知事件
    _notify(current);
  }

  //通知页面变化
  void _notify(RouteStatusInfo current) {
    //判断是否在home页面,如果在的话切换的就是底部tab
    if (current.page is BottomNavigator && _bottomTab != null) {
      //在主页时切换了tab
      current = _bottomTab!;
    }
    print("h_n_:current::${current.page}");
    print("h_n_:_current::${_current?.page}");
    _listener.forEach((listener) {
      //传入当前打开的页面,和上次打开的页面
      listener(current, _current);
    });
    _current = current;
  }
}

///抽象类,供HiNavigator实现
abstract class _RouteJumpListener {
  void onJumpTo(RouteStatus routeStatus, {Map<String, dynamic>? args});
}

typedef OnJumpTo =
    void Function(RouteStatus routeStatus, {Map<String, dynamic>? args});

///定义路由跳转逻辑要实现的功能
class RouteJumpListener {
  final OnJumpTo? onJumpTo;

  RouteJumpListener({this.onJumpTo});
}
